﻿using System;
using WiMo.Games;
using WiMo.Games.Drawables;
using WiMo.Games.Inputs;

namespace Spring
{
    public class Game : IGame
    {
        private ISpriteImage _ball;
        private IInputManager _lastState;

        public Game(IGameEngine engine)
        {
            Engine = engine;
            Engine.ContentManager.ContentLocation = "Content";
            Engine.Display.BackgroundColor = Color.Yellow;
            Engine.EnableDiagnostics = true;
            Engine.DiagnosticsFontColor = Color.Black;
        }

        public void Initialize()
        {
            Engine.PhysicsManager.Gravity = new Vector2(2, 10);
            Engine.InputManager.RightSoftButton = "Exit";
            Engine.InputManager.LeftSoftButton = "Release";
            Engine.InputManager.MenuBarBackgroundColor = Color.Black;
            Engine.InputManager.SoftMenuFontColor = Color.White;
        }

        public IGameEngine Engine { get; set; }
        public void BeginRun() {}
        public void CreateMenus() {}

        public void LoadContent()
        {
            _ball = Engine.ContentManager.LoadImage("Ball.png");
            _ball.Position = new PointF(Engine.Display.Size.Width / 2, Engine.Display.Size.Height / 2);
            _ball.ColorKey = Color.White;

            var springConstant = 20.0f;
            var dampingCoefficient = 0.5f;

            _ball.Spring = new WiMo.Games.MathLib.Objects.Spring(_ball, dampingCoefficient, springConstant);

            Engine.PhysicsManager.Add(_ball);
            Engine.CollisionManager.Add(_ball);
        }

        public void Update(GameTime gameTime)
        {
            if (Engine.InputManager.IsKeyDown(Keys.RightSoftButton))
                Engine.Exit();

            if (Engine.InputManager.WasPressed(_lastState, Keys.LeftSoftButton))
                _ball.Spring.Release(gameTime);

            _lastState = Engine.InputManager.GetKeysState();

            Engine.PhysicsManager.Update(gameTime);
        }

        public void Draw(GameTime gameTime)
        {
            Engine.Display.Add(_ball);
        }

        public bool CanExit()
        {
            return true;
        }

        
    }
}
